package com.example.administrator.animationdemo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;

public class TipCircleViewGroup extends ViewGroup {

    public TipCircleViewGroup(Context context) {
        super(context);
    }

    public TipCircleViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = getWidth();
        int height = getHeight();
        int centerX = width / 2;
        int radius = (int) ((height * 0.7f) / 2);
        int space = (int) (radius * 2 * 0.7f); // 2个圆环之间的间隔
        int halfSpace = space / 2;
        int x, y = height / 2;
        int[] arrayInt = new int[]{0, -2, -4, -4, -2, 0};
        for (int i = 0; i < 6; i++) {
            TipCircleView child = (TipCircleView) getChildAt(i);
            x = centerX + (i < 3 ? -1 : 1) * (radius + halfSpace) * (5 + arrayInt[i]);
            child.setInitX(x);
            child.setInitY(y);
            child.setCurrentX(x);
            child.setCurrentY(y);
            child.setInitR(radius);
            child.layout(x - radius, y - radius, x + radius, y + radius);
        }
    }

    public void moveLayout(boolean directionL, int step) {
        synchronized (this) {
            for (int i = 0; i < 6; i++) {
                TipCircleView child = (TipCircleView) getChildAt(i);
                int x = child.getCurrentX();
                int y = child.getCurrentY();
                int radius = child.getInitR();

                if (directionL)
                    x -= step;
                else
                    x += step;

                child.setCurrentX(x);
                child.layout(x - radius, y - radius, x + radius, y + radius);
            }
        }
    }

    public void fillCircles(int inputFlag) {
        for (int i = 0; i < 6; i++) {
            final TipCircleView circleView = (TipCircleView) getChildAt(i);

            if (i <= inputFlag)
                circleView.setFill(true);
            else
                circleView.setFill(false);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    circleView.postInvalidate();
                }
            }).start();
        }
    }

    public void reset() {
        for (int i = 0; i < 6; i++) {
            final TipCircleView child = (TipCircleView) getChildAt(i);
            child.setFill(false);
            int x = child.getInitX();
            int y = child.getInitY();
            int radius = child.getInitR();
            child.setCurrentX(x);
            child.setCurrentY(y);
            child.layout(x - radius, y - radius, x + radius, y + radius);
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    child.postInvalidate();
                }
            });
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
